<?php
/**
 * 标签显示数据处理
 *
 * User: kaikaic
 * Date: 2018/4/20
 * Time: 0:10
 */

namespace app\admin\model;

use think\Db;

class TagsModel {

    protected static $table = "SystemTags";

    /**
     * 分类数据
     */
    public static $categorys;

    /**
     * 列出全部tag
     */
    public static function listTags() {
        return Db::name(self::$table)->select();
    }

    /**
     * 按分类合并标签
     */
    public static function combineTagsByCategory() {
        $tagList = self::listTags();

        $tagArray = array();
        foreach (self::$categorys as $key=>$val) {
            $tagArray[$key]['tags'] = array();
            $tagArray[$key]['name'] = $val;
        }

        for ($i=0; $i<count($tagList); $i++) {
            $categoryID = $tagList[$i]['category_id'];
            if (!isset($tagArray[$categoryID])) {
                continue;
            }

            $tagArray[$categoryID]['tags'][] = $tagList[$i];
        }

        foreach ($tagArray as $key=>$val) {
            $tagArray[$key]['tags'] = array_chunk($val['tags'], 6);
        }

        return $tagArray;
    }
}
